package com.sky.controller.admin;

import com.sky.annotation.Permission;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageBean;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.service.impl.DishServiceImpl;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
@Slf4j
public class DishController {
    @Autowired
    DishService dishService;

    /**
     * 添加一个新的菜品
     * @param dto
     * @return
     */
    @PostMapping
    @ApiOperation("添加一个新的菜品")
    public Result add(@RequestBody DishDTO dto) {
        log.info("添加一个新的菜品{}", dto);
         dishService.add(dto);
         return Result.success();
    }
    /**
     * 菜品分页查询
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result listByPage( DishPageQueryDTO dto){
        log.info("菜品分页查询{}", dto);
        PageBean<DishVO> pageBean= dishService.listByPage(dto);
        return Result.success(pageBean);
    }
    /**
     * 删除菜品
     */
    @DeleteMapping
    @ApiOperation("删除菜品")
    @Permission(Value = "delete")
    public Result delete(@RequestParam("ids") List<Long> ids){  //需要用到参数绑定
        log.info("删除菜品的参数{}",ids );
        dishService.delete(ids);
        return Result.success();
    }
    /**
     * 根据id查找菜品
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id){
        log.info("根据id查找菜品{}", id);
       DishVO dish= dishService.getById(id);
        return Result.success(dish);
    }
    /**
     * 修改菜品
     */
    @PutMapping
    public Result update(@RequestBody DishDTO dish){
        log.info("更新数据{}",dish);
        dishService.update(dish);
        return Result.success();
    }

    /**
     * 更改菜单的状态
     * @param status
     * @return
     */
    @PostMapping("/status/{status}")
    public Result changeStatus(@PathVariable Integer status,Long id){
        log.info("更改菜单状态{}，id{}",status,id);
        dishService.changeStatus(status,id);
        return Result.success();
    }
    /**
     * 根据分类id查询菜品
     */
    @GetMapping("/list")
    public Result listByCategoryId( Long categoryId){
        log.info("根据菜品套餐查询{}",categoryId);
        List<Dish> dish= dishService.listByCategoryId(categoryId);
        return Result.success(dish);
    }


}
